<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="通知主题" prop="title">
        <el-input v-model="dataForm.title" maxlength="50" show-word-limit placeholder="请填写通知主题"></el-input>
      </el-form-item>

      <el-form-item label="通知内容" prop="sketch">
        <el-input type="textarea" maxlength="500" show-word-limit :autosize="{ minRows:4, maxRows: 6}"
          v-model="dataForm.sketch"></el-input>
      </el-form-item>

      <el-form-item label="通告天数" prop="day">

        <el-row>
          <el-col :span="22">
            <el-input v-model="dataForm.day" placeholder="请填写数字天数"></el-input>
          </el-col>

          <el-col :span="2" class="icon-list__tips">
            <el-tooltip placement="top" effect="light">
              <div slot="content">如果不填写通告天数的话，就默认为永久通告</div>
              <i class="el-icon-warning"></i>
            </el-tooltip>
          </el-col>
        </el-row>

      </el-form-item>

      <el-form-item label="是否置顶" prop="isTop">
        <el-switch v-model="dataForm.isTop" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
          inactive-value="0" />
      </el-form-item>

      <el-form-item label="附件" prop="file">
        <single-upload v-model="dataForm.fileUrl" ref="singleUpload" @change="costPlannedAmountChange"></single-upload>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import SingleUpload from '@/components/upload/singleUpload'
  export default {
    data() {
      var validateDay = (rule, value, callback) => {
        if (this.dataForm.day !== undefined && isNaN(this.dataForm.day)) {
          callback(new Error('不是有效数字，请填写数字！'))
        } else if (this.dataForm.day > 100) {
          callback(new Error('通告天数不能大于100'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: undefined,
          sketch: undefined,
          day: undefined,
          isTop: '0',
          fileUrl: undefined,
          fileName: undefined
        },
        dataRule: {
          title: [
            { required: true, message: '通知主题不能为空', trigger: ['change', 'blur'] }
          ],
          sketch: [
            { required: true, message: '通告内容不能为空', trigger: ['change', 'blur'] }
          ],
          day: [
            { required: false, validator: validateDay, trigger: 'change' }
          ]
        }
      }
    },
    components: {
      SingleUpload
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.$nextTick(() => {
          this.$refs.singleUpload.reset()
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/examineapprove/notice/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = { ...data.notice }
              if (data.notice.isTop === 0) {
                this.dataForm.isTop = '0'
              } else if (data.notice.isTop === 1) {
                this.dataForm.isTop = '1'
              }
              this.$nextTick(() => {
                this.$refs.singleUpload.change(data.notice.fileName, data.notice.fileUrl)
              })
            }
          })
        }
        this.visible = true
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/examineapprove/notice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'sketch': this.dataForm.sketch,
                'day': this.dataForm.day,
                'isTop': this.dataForm.isTop,
                'fileUrl': this.dataForm.fileUrl,
                'fileName': this.dataForm.fileName
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      async costPlannedAmountChange(url, name) {
        this.dataForm.fileUrl = url
        this.dataForm.fileName = name
      }
    }
  }
</script>